package dynamicProgramming;

import java.util.Scanner;

/**
 * @program: data_structures_algorithms
 * @description: 母牛从3-7岁初每年会生产1头小母牛，10岁后死亡
 * ，假设初始有1头刚出生的母牛，请问第n年有多少头母牛？（年从第一年开始计数）
 * @author: lld
 * @create: 2021-04-16 17:11
 **/
public class CowProduction {
    public static void main(String[] args) {
        System.out.println("输入第几年：");
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int cowNum = getCowNum(i);
        System.out.printf("输入第%d年共有：%d", i, cowNum);
    }

    public static int getCowNum(int n) {
        if (n < 3) {
            return 1;
        }
        int[] dp = new int[n + 1];
        dp[0] = 0;
//        前2年不生产
        dp[1] = 1;
        dp[2] = 1;
        int sum = 0;
        for (int i = 3; i <= n; i++) {
            if (i >= 3 && i <= 7) {
                sum = sum + dp[i - 2];
                dp[i] = sum + 1;//加上
            } else if (i >= 8 && i <= 10) {
                sum = sum + dp[i - 2] - dp[i - 7];
                dp[i] = sum + 1;
            } else {
                sum = sum + dp[i - 2] - dp[i - 7];
                dp[i] = sum;
            }
        }
        return dp[n];
    }
}
